import ScrollViewToolParent from "./ScrollViewToolParent";

const { ccclass, property, menu, disallowMultiple } = cc._decorator;

/**
* 分段查询,scroolView 辅助工具类
*/
@ccclass
@menu("自定义组件/ScrollView/分段查询")
@disallowMultiple
export default class SegmentedQueryScrollView extends ScrollViewToolParent {

    /** API接口
     * 
     * @param dataCount 当前数据长度
     * @param callback 子节点赋值回调
     * @param continueQueryCallback 继续查询回调
     * @param itemIsInit 节点使用时是否执行了数据初始化
     */
    setData(
        dataCount: number,
        callback: (node: cc.Node, index: number, isDynamic: boolean) => void,
        continueQueryCallback: (index: number, /** 查询完毕后刷新数据个数回调*/refreshDataCountCallback: (dataCount: number) => void) => void,
        itemIsInit: boolean = false
    ) {
        this._initAllItem()
        if (dataCount == 0) {
            return
        }
        this._callback = callback
        this._continueQueryCallback = continueQueryCallback;
        this._dataCount = dataCount
        this._itemIsInitByCallBack = itemIsInit



        this.scrollview.stopAutoScroll()

        this._refreshUnUseChildData(true)
        this._content.y = 0;
    }


    /** 获取新的数据
     * 
     * @param index 当前index值
     * @override true
     */
    _getNewData(index, isTop = false) {
        cc.log("獲取新的數據 children")

        if (this._continueQueryCallback) {
            //(到最后一个数据 || 到第一个数据 )&&  没有在查询 
            if ((index >= this._dataCount - 1 || (isTop && index == 0)) && !this._notQuerying) {
                this._notQuerying = true
                this._continueQueryCallback(index, this._refreshDataCount.bind(this))
            }
        }
    }

    /** 刷新数据个数 */
    _refreshDataCount(dataCount) {
        this._notQuerying = false
        this._dataCount = dataCount
        this._refreshUnUseChildData(false)
    }


}
